﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using AKMII.DMRA.Business.Management;
using AKMII.DMRA.Business.Rule;
using AKMII.DMRA.Common;
using System.Threading;
using System.Collections.Generic;
using System.Xml.Serialization;
using AKMII.DMRA.Business.Service;
using System.Web.Services;
namespace AKMII.DMRA.Web.MDPages
{
    public partial class AgentPotential : System.Web.UI.Page
    {
        public int agentID;
        protected void Page_Load(object sender, EventArgs e)
        {
            agentID = int.Parse(Request.QueryString[0]);
            if (!IsPostBack)
            {

                DataBind(0.55, 0.35, agentID);
            }
        }
        [WebMethod]
        public static bool sendMail(string content)
        {
            try
            {
                AccountManager account = new AccountManager();
                string mailAddress = account.GetMailAddress(HttpContext.Current.Session["UserName"].ToString());
                Mail mail = new Mail();
                mail.Subject = "Agent potential report";
                mail.Content = content;
                mail.Recipients = new List<string>() { mailAddress };
                Thread thread = new Thread(new ParameterizedThreadStart(Send));
                thread.Start(mail);
                return true;
            }
            catch
            {
                return false;
            }

        }
        public static void Send(object o)
        {
            Mail mail = o as Mail;
            if (mail != null)
            {
                CommonUtility.SendMail(mail);
            }
        }
        private void DataBind(double classRate, double classRate2, int agentID)
        {
            AccountManager accountManager = new AccountManager();
            Assignment assignment = accountManager.GetAssignment(User.Identity.Name.ToString(), agentID);
            assignment.ClassRate = classRate;
            assignment.ClassRate2 = classRate2;
            this.lblAAdv.Text = "$"+assignment.AAdvancedIncome.ToString();
            this.lblAAnn.Text = "$"+assignment.AAnnualizedIncome.ToString();
            this.lblAApp.Text = assignment.AAppointMade.ToString();
            this.lblAAPPK.Text = assignment.AAppointKept.ToString();
            this.lblAAvg.Text = "$"+assignment.AAvgAPV.ToString();
            this.lblACards.Text = assignment.AOpportunity.ToString();
            this.lblACon.Text = assignment.AContacts.ToString();
            this.lblAEst.Text = "$"+assignment.AEstimatedAPV.ToString();
            this.lblAPre.Text = assignment.APresentation.ToString();
            this.lblAsale.Text = assignment.AAppointSale.ToString();

            this.lblLAdv.Text = "$"+assignment.LAdvancedIncome.ToString();
            this.lblLAnn.Text = "$"+assignment.LAnnualizedIncome.ToString();
            this.lblLApp.Text = assignment.LAppointMade.ToString();
            this.lblLAPPK.Text = assignment.LAppointKept.ToString();
            this.lblLAvg.Text = "$"+assignment.LAvgAPV.ToString();
            this.lblLCars.Text = assignment.LOpportunity.ToString();
            this.lblLCon.Text = assignment.LContacts.ToString();
            this.lblLEst.Text = "$"+assignment.LEstimatedAPV.ToString();
            this.lblLPre.Text = assignment.LPresentation.ToString();
            this.lblLsale.Text = assignment.LAppointSale.ToString();

            this.lblNAdv.Text = "$"+assignment.NAdvancedIncome.ToString();
            this.lblNAnn.Text = "$"+assignment.NAnnualizedIncome.ToString();
            this.lblNApp.Text = assignment.NAppointMade.ToString();
            this.lblNAPPK.Text = assignment.NAppointKept.ToString();
            this.lblNAvg.Text = "$"+assignment.NAvgAPV.ToString();
            this.lblNCars.Text = assignment.NOpportunity.ToString();
            this.lblNCon.Text = assignment.NContacts.ToString();
            this.lblNEst.Text = "$"+assignment.NEstimatedAPV.ToString();
            this.lblNPre.Text = assignment.NPresentation.ToString();
            this.lblNSale.Text = assignment.NAppointSale.ToString();

            this.lblBAdv.Text = "$"+assignment.BAdvancedIncome.ToString();
            this.lblBAnn.Text = "$"+assignment.BAnnualizedIncome.ToString();
            this.lblBApp.Text = assignment.BAppointMade.ToString();
            this.lblBAPPK.Text = assignment.BAppointKept.ToString();
            this.lblBAvg.Text = "$"+assignment.BAvgAPV.ToString();
            this.lblBCars.Text = assignment.BOpportunity.ToString();
            this.lblBCon.Text = assignment.BContacts.ToString();
            this.lblBEst.Text = "$"+assignment.BEstimatedAPV.ToString();
            this.lblBPre.Text = assignment.BPresentation.ToString();
            this.lblBSale.Text = assignment.BAppointSale.ToString();

            this.lblCAdv.Text = "$"+assignment.CCPAdvancedIncome.ToString();
            this.lblCAnn.Text = "$"+assignment.CCPAnnualizedIncome.ToString();
            this.lblCApp.Text = assignment.CCPAppointMade.ToString();
            this.lblCAPPK.Text = assignment.CCPAppointKept.ToString();
            this.lblCAvg.Text = "$"+assignment.CCPAvgAPV.ToString();
            this.lblCCars.Text = assignment.CCPOpportunity.ToString();
            this.lblCCon.Text = assignment.CCPContacts.ToString();
            this.lblCEst.Text = "$"+assignment.CCPEstimatedAPV.ToString();
            this.lblCPre.Text = assignment.CCPPresentation.ToString();
            this.lblCSale.Text = assignment.CCPAppointSale.ToString();

            this.lblRAdv.Text = "$"+assignment.RAdvancedIncome.ToString();
            this.lblRAnn.Text = "$"+assignment.RAnnualizedIncome.ToString();
            this.lblRApp.Text = assignment.RAppointMade.ToString();
            this.lblRAPPK.Text = assignment.RAppointKept.ToString();
            this.lblRAvg.Text = "$"+assignment.RAvgAPV.ToString();
            this.lblRCars.Text = assignment.ROpportunity.ToString();
            this.lblRCon0.Text = assignment.RContacts.ToString();
            this.lblREst.Text = "$"+assignment.REstimatedAPV.ToString();
            this.lblRPre.Text = assignment.RPresentation.ToString();
            this.lblRSale.Text = assignment.RAppointSale.ToString();

            this.lblBusAdv0.Text = "$"+assignment.TSAdvancedIncome.ToString();
            this.lblBusAnn.Text = "$"+assignment.TSAnnualizedIncome.ToString();
            this.lblBusApp.Text = assignment.TSAppointMade.ToString();
            this.lblBusAPPK.Text = assignment.TSAppointKept.ToString();
            this.lblBusAvg.Text = "$"+assignment.TSAvgAPV.ToString();
            this.lblBusCars.Text = assignment.TSOpportunity.ToString();
            this.lblBusCon.Text = assignment.TSContacts.ToString();
            this.lblBusEst.Text = "$"+assignment.TSEstimatedAPV.ToString();
            this.lblBusRPre.Text = assignment.TSPresentation.ToString();
            this.lblBusSale.Text = assignment.TSAppointSale.ToString();

            this.lblTAdv0.Text ="$"+ assignment.TMAdvancedIncome.ToString();
            this.lblTAnn.Text = "$"+assignment.TMAnnualizedIncome.ToString();
            this.lblTApp.Text = assignment.TMAppointMade.ToString();
            this.lblTAPPK.Text = assignment.TMAppointKept.ToString();
            this.lblTAvg.Text = "$"+assignment.TMAvgAPV.ToString();
            this.lblTcars.Text = assignment.TMOpportunity.ToString();
            this.lblTCon.Text = assignment.TMContacts.ToString();
            this.lblTEst.Text = "$"+assignment.TMEstimatedAPV.ToString();
            this.lblTRPre.Text = assignment.TMPresentation.ToString();
            this.lblTSale.Text = assignment.TMAppointSale.ToString();

            this.lblFAdv.Text = "$"+assignment.FRAdvancedIncome.ToString();
            this.lblFAnn.Text = "$"+assignment.FRAnnualizedIncome.ToString();
            this.lblFApp.Text = assignment.FRAppointMade.ToString();
            this.lblFAPPK.Text = assignment.FRAppointKept.ToString();
            this.lblFAvg.Text = "$"+assignment.FRAvgAPV.ToString();
            this.lblFCars.Text = assignment.FROpportunity.ToString();
            this.lblFCon.Text = assignment.FRContacts.ToString();
            this.lblFEst.Text = "$"+assignment.FREstimatedAPV.ToString();
            this.lblFPre.Text = assignment.FRPresentation.ToString();
            this.lblFSale.Text = assignment.FRAppointSale.ToString();

            this.lblAssignAdv.Text = "$"+assignment.APAdvancedIncome.ToString();
            this.lblAssignAnn.Text = "$"+assignment.APAnnualizedIncome.ToString();
            this.lblAssignApp.Text = assignment.APAppointMade.ToString();
            this.lblAssignAPPK.Text = assignment.APAppointKept.ToString();
            this.lblAssignAvg.Text = "$"+assignment.APAvgAPV.ToString();
            this.lblAssignCars.Text = assignment.APOpportunity.ToString();
            this.lblAssignCon.Text = assignment.APContacts.ToString();
            this.lblAssignEst.Text = "$"+assignment.APEstimatedAPV.ToString();
            this.lblAssignRPre.Text = assignment.APPresentation.ToString();
            this.lblAssignSale.Text = assignment.APAppointMade.ToString();

            #region Changement added by Andy
            //Cards Assigned     -- Minimum Lead Potential
            this.lblMLCards.Text = (assignment.AOpportunity + assignment.LOpportunity + assignment.NOpportunity+assignment.BOpportunity).ToString();
            //Contacts / FF      -- Minimum Lead Potential
            this.lblMLCon.Text = (assignment.AContacts + assignment.LContacts+assignment.NContacts+assignment.BContacts).ToString();
            //Presentations      -- Minimum Lead Potential
            this.lblMLPre.Text = (assignment.ActivePresentation + assignment.LPresentation + assignment.NPresentation + assignment.BPresentation).ToString();
            //Sales(apps)        -- Minimum Lead Potential
            this.lblMLSale.Text = (assignment.AAppointSale + assignment.LAppointSale + assignment.NAppointSale + assignment.BAppointSale).ToString();
            //Estimated APV      -- Minimum Lead Potential
            this.lblMLEst.Text = "$" + (assignment.AEstimatedAPV + assignment.LEstimatedAPV + assignment.NEstimatedAPV + assignment.BEstimatedAPV).ToString();
            //Annualized Income  -- Minimum Lead Potential
            this.lblMlAnn.Text = "$" + (assignment.AAnnualizedIncome + assignment.LAnnualizedIncome + assignment.NAnnualizedIncome + assignment.BAnnualizedIncome).ToString();
            //Advanced Income    -- Minimum Lead Potential
            this.lblMLAdv.Text = "$" + (assignment.AAdvancedIncome + assignment.LAdvancedIncome + assignment.NAdvancedIncome + assignment.BAdvancedIncome).ToString();
            //Avg APV per App    -- Minimum Lead Potential
            int avg = 0;
            try
            {
                avg = (assignment.AEstimatedAPV + assignment.LEstimatedAPV + assignment.NEstimatedAPV + assignment.BEstimatedAPV) / (assignment.AAppointSale + assignment.LAppointSale + assignment.NAppointSale + assignment.BAppointSale);
            }
            catch
            {}
            this.lblMLAvg.Text = (avg == 0) ? "" : "$" + avg.ToString();
            #endregion
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.DropDownList1.SelectedIndex == 0)
            {
                DataBind(0.55, 0.35, agentID);
            }
            else
            {
                DataBind(0.45, 0.3, agentID);
            }
        }
    }
}
